/*******************************************************************************
 * Copyright (c) Jonathan Buck - jonbuck16@gmail.com
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    jonbuck16@gmail.com - initial API and implementation
 *******************************************************************************/
package net.jonbuck.tassoo.ui.validation;

import org.apache.commons.lang.StringUtils;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.databinding.validation.MultiValidator;
import org.eclipse.core.databinding.validation.ValidationStatus;
import org.eclipse.core.runtime.IStatus;

/**
 * 
 * 
 * @since 1.0.0
 */
public class IsSubTaskValidator extends MultiValidator {

	private IObservableValue isSubTaskButton;

	private IObservableValue parentTaskField;

	/**
	 * 
	 * @param isSubTaskButton
	 * @param parentTaskField
	 */
	public IsSubTaskValidator(IObservableValue isSubTaskButton, IObservableValue parentTaskField) {
		super();
		this.isSubTaskButton = isSubTaskButton;
		this.parentTaskField = parentTaskField;
	}

	/**
	 * 
	 */
	protected IStatus validate() {
		Boolean isSubTask = (Boolean) isSubTaskButton.getValue();
		String parentTaskName = (String) parentTaskField.getValue();

		if (isSubTask && StringUtils.isBlank(parentTaskName)) {
			return ValidationStatus.error("If the task is a subtask then a parent must be specified");
		}

		return ValidationStatus.ok();
	}

}
